package com.service.business.support;

import com.service.business.base.constants.RedisLockConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;

/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年05月21日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Component
public class OrderLockUtils extends RedissonLockUtils {

    private String KEY_PREFIX = "order_";

    public boolean lockProductOtherOrder(@NotNull String orderNo) {
        return lockOrder(buildOtherOrderLockKey(orderNo));
    }

    public boolean lockProductHrOrder(@NotNull String orderNo) {
        return lockOrder(buildHrOrderLockKey(orderNo));
    }

    public String buildOtherOrderLockKey(String orderNo) {
        return RedisLockConstant.product_other_order + orderNo;
    }

    public String buildHrOrderLockKey(String orderNo) {
        return RedisLockConstant.product_hr_order + orderNo;
    }

    public String buildLockOrderLockKey(String lockNo) {
        return RedisLockConstant.product_lock_order + lockNo;
    }

    public boolean lockOrder(@NotNull String key) {
        return this.lock(key);
    }

    public void unlockProductOtherOrder(@NotNull String orderNo) {
        this.unlockOrder(buildOtherOrderLockKey(orderNo));
    }

    public void unlockProductHrOrder(@NotNull String orderNo) {
        this.unlockOrder(buildHrOrderLockKey(orderNo));
    }

    public void unlockOrder(@NotNull String key) {
        this.unlock(key);
    }

}
